﻿#!/usr/bin/env python
# -*- coding: UTF-8-*-
'''
    Use python to access operating system commands
    
    Read Text Extension for OpenOffice.org (for Linux) :
    See: http://sites.google.com/site/readtextextension/
    Tools > Add-Ons > Read Selection... Dialog setup:
    External program: /usr/bin/python
    Command line: "(OS_PY)" --c "espeak -f '(TMP)'"
    Copyright (c) 2011 James Holgate
    '''
import getopt, sys, os, readtexttools

def usage():
    sA = ' ' + os.path.split(sys.argv[0])[1]
    print ("")
    print ("Usage")
    print (sA + " --c ""string"" ")
    print ("")

def playscript(s1):
    try: 
        print ("> "+ s1)
        readtexttools.myossystem(s1)
    except err:
        print ('I was unable to do the command!')
        print (str(err))
        usage()
        sys.exit(2)

def main():
    s1=""
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hc", ["help", "command="])
    except getopt.GetoptError, err:
        # print help information and exit:
        print (str(err)) # will print something like "option -a not recognized"
        usage()
        sys.exit(2)
    for o, a in opts:
        if o in ("-h", "--help"):
            usage()
            sys.exit(0)
        elif o in ("-c", "--command"):
            s1=a
        else:
            assert False, "unhandled option"
    if (s1==""):
        s1=sys.argv[-1]
    s2=s1.encode( "utf-8" )
    playscript(s2)
    sys.exit(0)

if __name__ == "__main__":
    main()
